package lab03.test13;

import java.io.*;
import java.sql.*;

public class Test13 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = null;
        Statement statement = null;

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC&characterEncoding=gbk", "root", "123456");
            statement = connection.createStatement();
            //创建基本表
            statement.execute("CREATE TABLE userinfo" +
                    " (usercode VARCHAR (4) not NULL," +
                    "username VARCHAR (20)," +
                    "password VARCHAR (6)," +
                    "email VARCHAR (40)," +
                    "PRIMARY KEY (usercode))");

            //向数据库中插入数据
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7101','宋晓梅','123456','xiaomei@yahoo.com.cn');");


            //输出用户数据
            ResultSet resultSet = statement.executeQuery("SELECT * FROM userinfo ");
            while (resultSet.next() == true) {
                System.out.print(resultSet.getString(1) + "  ");
                System.out.print(resultSet.getString(2) + "  ");
                System.out.print(resultSet.getString(3) + "  ");
                System.out.println(resultSet.getString(4));
            }

            //插入记录
            System.out.println("更新数据：");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7107','张三','633321','zhangsan@263.net');");


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
